package com.goods.business.controller;


import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 类别列表展示接口(分页)
     */
    @GetMapping("categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> loadCategoryTree(
            @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> categoryTree = productCategoryService.getCategoryTree(pageNum, pageSize);
        return ResponseBean.success(categoryTree);
    }

    /**
     * 查询父分类
     */
    @GetMapping("getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> parentCategoryTree = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(parentCategoryTree);
    }


    /**
     * 添加分类
     */
    @PostMapping("add")
    public ResponseBean addCategory(@RequestBody ProductCategoryVO productCategoryVO) {
        productCategoryService.addCategory(productCategoryVO);
        return ResponseBean.success();
    }

    @GetMapping("edit/{id}")
    public ResponseBean<ProductCategoryVO> editCategory(@PathVariable Integer id) {
        ProductCategoryVO productCategoryVO = productCategoryService.getCategoryById(id);
        return ResponseBean.success(productCategoryVO);
    }

    @PutMapping("update/{id}")
    public ResponseBean updateCategory(@PathVariable Integer id, @RequestBody ProductCategoryVO productCategoryVO) {
        productCategoryService.updateCategory(productCategoryVO);
        return ResponseBean.success();
    }

    @DeleteMapping("delete/{id}")
    public ResponseBean deleteCategory(@PathVariable Integer id) throws BusinessException {
        productCategoryService.deleteCategory(id);
        return ResponseBean.success();
    }
}

